import request from '@/utils/request.js'
import useUserInfoStore from '@/store/user.js'
import Socket from '@/utils/websocket.js'

export default {
	data() {
		return {
			user: null,
			entitle: '',
			chtitle: ''
		}
	},
	mounted() {
		try {
			const pages = getCurrentPages()
			const page = pages[pages.length - 1]
			const t = page.$page.meta.navigationBar.titleText
			this.chtitle = this.chtitle || t
			uni.setNavigationBarTitle({
				title: this.$i(this.chtitle, this.entitle || this.chtitle)
			})
		} catch (e) {
			//TODO handle the exception
		}
	},
	computed: {
		formatData() {
			return (timestamp)=>{
				timestamp = timestamp ? timestamp : null;
				let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let Y = date.getFullYear() + '-';
				let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
				let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
				let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
				let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
				return Y + M + D + h + m + s;
			}
		},
		language() {
			return this.user.language
		},
		userInfo() {
			return this.user.userInfo
		},
		$i() {
			return (ch, en) => {
				if (this.user.language == 'ch') {
					return ch
				} else {
					return en
				}
			}
		},
		URL() {
			return (url) => {
				if (url && (url.includes(',') && !url.includes('base64'))) url = url.split(',')[0]
				return url ? ((url.includes('http') || url.includes('data:')) ? url : this.user.imgURL + url) : ''
			}
		},
	},
	created() {
		this.user = useUserInfoStore()
	},
	methods: {
		$http: request,
		to(url, type = 0) {
			if (type == 1) {
				uni.switchTab({
					url: url
				})
			} else {
				uni.navigateTo({
					url: url
				})
			}
		},
		msg(title,title2){
			uni.showToast({
				icon:'none',
				title:this.$i(title,title2)
			})
		},
		WebSocketClose() {
			this.user.ws.closeSocket()
		},
		WebSocketInit() {
			// const wsURL='ws://localhost:8989'
			// const wsURL = 'ws://zyue.ltd:8989'
			const wsURL = 'wss://aikf.fuliaoxx.com'
			this.user.ws = new Socket(wsURL, () => {
				this.WebSocketOpen();
			})
			this.user.ws.getWebSocketMsg(data => {
				this.WebSocketOnmessage(data)
			});
		},
		WebSocketOpen() {
			this.WebSocketSend({
				appkey: 'fQC3QCsjw8yXkAs3i4qJkg=='
			}, 'login')
		},
		clearUnread() {

		},
		WebSocketOnmessage(data) {
			if (data.type == 'chat') {
				this.user.messageList.push({
					...data,
					time: new Date().getTime()
				})
				this.user.unread++
				this.clearUnread()
			}
		},
		
		async getUserInfo(){
			console.log("获取用户信息")
			const {
				data,
				code
			} = await this.$http({
				url: '/api/user/index',
			})
			if (code == 1) {
				this.user.setData('userInfo', data)
			}
		},
		WebSocketSend(data, type = 'chat') {
			const params = {
				uid: this.userInfo.id + '',
				// uid:'123456',
				to_uid: '1',
				data: {
					...data,
					username: this.userInfo.username,
					avatar: this.userInfo.avatar
				},
				type: type
			}
			if (type == 'chat') {
				this.user.messageList.push({
					...params,
					time: new Date().getTime()
				})
			}
			this.user.ws.webSocketSendMsg(params)
		},
	}
}